import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class PieWindow extends GraphWindow {
        
    public PieWindow(int numSlices) {
    	super(numSlices);
    }
    
    // This method is called whenever the contents needs to be painted
    @Override
    public void paint(Graphics g){
    	// Get total value of all slices
        double total = 0.0D;
        for (int i=0; i<slices.length; i++) {
            total += slices[i].value;
        }
        Rectangle area = getBounds();
        // Draw each pie slice
        double curValue = 0.0D;
        int startAngle = 0;
        for (int i=0; i<slices.length; i++) {
            // Compute the start and stop angles
            startAngle = (int)(curValue * 360 / total);
            int arcAngle = (int)(slices[i].value * 360 / total);

            // Ensure that rounding errors do not leave a gap between the first and last slice
            if (i == slices.length-1) {
                arcAngle = 360 - startAngle;
            }

            // Set the color and draw a filled arc
            g.setColor(slices[i].color);
            g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);

            curValue += slices[i].value;
        }
    }
    
    @Override
    public void calculate(String[] a, int top, DataProcessor dp) {
    	// this block of code figures out how many total tweets contain the top n words
        double[] amounts = new double[top];
        int sum = 0;
        for (int i = 0; i < amounts.length; i++) {
       	 sum += dp.numTweetsWithWord(a[i]);
        }
        // then figures out the percent each top 5 words shares
        System.out.println(sum);
        for (int i = 0; i < top; i++) {
       	 amounts[i] = dp.numTweetsWithWord(a[i]) / (double)sum;
       	 System.out.println(amounts[i] +" " + dp.numTweetsWithWord(a[i]));
        }
        // thus ensures it adds up to 100%, otherwise just calling dp.pctTweetsWithWord > 100%
        Random rand = new Random();
        System.out.println("Top " + top + " Words Pie:");
        for (int i = 0; i < top; i++) {
       	 System.out.println(a[i] +" " + 100 * amounts[i]+ "%");
       	 // get next value out of 100 and put random color in
            addSlice(amounts[i], new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
        }
    }
}

